package com.blog.web.front;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.blog.constant.Constants;
import com.blog.model.Page;
import com.blog.model.TableJson;
import com.blog.pojo.Article;
import com.blog.pojo.Cate;
import com.blog.service.ArticleService;
import com.blog.util.PageUtil;

/** 
 * @author ant 
 * @date 创建时间：2017年8月1日 下午8:51:58  
 */
@Controller
public class ArticleConttroller {
	
	@Autowired
	private ArticleService articleService;
	
	@ModelAttribute
	public void getCateList(Model model){
		List<Cate> cateList = articleService.listAllCate(Constants.CATE_ACTIVE);
		List<Article> articleList = articleService.selectByViewCount(Constants.VIEW_SIZE_TEN);
		model.addAttribute("articleList", articleList);
		model.addAttribute("cateList", cateList);
	}
	
	@RequestMapping("/article")
	public String goToArticle(@ModelAttribute("articleTitle") String articleTitle,Integer pageNum,Model model) {
		Page page = new Page();
		if (pageNum == null) {
			page.setOffset(0);
			pageNum = 1;
		} else {
			int start = PageUtil.getStartIndex(Constants.PAGE_SIZE_TEN, pageNum);
			page.setOffset(start);
		}				
		page.setLimit(Constants.PAGE_SIZE_TEN);
		Article article = new Article();
		article.setArticleStatus(Constants.ARTICLE_ACTIVE);
		article.setArticleTitle(articleTitle);
		TableJson json = articleService.getArticleList(article, page);
		List<Integer> showPage = PageUtil.getPageList(json.getTotalPage(), pageNum);
		json.setShowPage(showPage);		
		model.addAttribute("json", json);
		model.addAttribute("pageNum", pageNum);		
		model.addAttribute("title", "技术文章");
		return "front/article/article";
	}
	
	@RequestMapping(value="/searchArticle",method=RequestMethod.POST)
	public String searchArticle(String articleTitle,RedirectAttributes model) {
		model.addAttribute("articleTitle", articleTitle);
		return "redirect:/article";		
	}
	
	@RequestMapping("/article/{articleId}")
	public String goToDetailArticle(@PathVariable Integer articleId,Model model) {
		Article article = articleService.getArticleByArticleId(articleId);
		Article previousArticle = articleService.getPreviousByPrimaryKey(articleId);
		Article nextArticle = articleService.getNextByPrimaryKey(articleId);
		model.addAttribute("article", article);
		model.addAttribute("previousArticle", previousArticle);
		model.addAttribute("nextArticle", nextArticle);
		return "front/article/detailArticle";
	}

	@RequestMapping("/article/cate/{cateId}")
	public String goToArticle(@PathVariable Integer cateId,Integer pageNum,Model model) {
		Page page = new Page();
		if (pageNum == null) {
			page.setOffset(0);
			pageNum = 1;
		} else {
			int start = PageUtil.getStartIndex(Constants.PAGE_SIZE_TEN, pageNum);
			page.setOffset(start);
		}				
		page.setLimit(Constants.PAGE_SIZE_TEN);
		Article article = new Article();
		article.setArticleStatus(Constants.ARTICLE_ACTIVE);
		Cate cate = articleService.getCateByCateId(cateId);
		article.setCate(cate);
		TableJson json = articleService.getArticleList(article, page);
		List<Integer> showPage = PageUtil.getPageList(json.getTotalPage(), pageNum);
		json.setShowPage(showPage);
		model.addAttribute("json", json);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("cate", cate);
		return "front/article/cate";
	}
}
